package school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import school.domain.Player;
import school.domain.Team;
import school.mapper.PlayerMapper;
import school.mapper.TeamMapper;
import school.support.BaseController;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("team")
public class TeamController extends BaseController {

    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private PlayerMapper playerMapper;

    /**
     * 球队列表
     *
     * @param team
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(Team team, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Team.class);
        if(team.getName()!=null)
        example.createCriteria()
                .andLike("name", "%" + team.getName() + "%");
        PageInfo<Team> pageInfo = new PageInfo<>(teamMapper.selectByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "team/list";
    }

    /**
     * 删除球队
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        teamMapper.deleteByPrimaryKey(id);
        return refresh();
    }

    /**
     * 添加球队
     *
     * @return
     */
    @GetMapping("add")
    public String add() {
        return "team/add";
    }

    /**
     * 添加球队
     *
     * @return
     */
    @PostMapping("add")
    public String add(Team team) {
        team.setAddtime(new Date());
        teamMapper.insertSelective(team);
        return "redirect:list.do";
    }

    /**
     * 球队详情
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {

        Team team = teamMapper.selectByPrimaryKey(id);
        model.addAttribute("team", team);
        return "team/detail";
    }

    /**
     * 更新球队信息
     *
     * @param team
     * @return
     */
    @PostMapping("update")
    public String update(Team team) {
        teamMapper.updateByPrimaryKeySelective(team);
        return "redirect:list.do";
    }

    /**
     * 球员信息
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("user/{id}")
    public String user(@PathVariable Integer id, Model model) {
        Player player = new Player();
        player.setTeamid(id);
        List<Player> playerList = playerMapper.select(player);
        model.addAttribute("playerList", playerList);
        Team team = teamMapper.selectByPrimaryKey(id);
        model.addAttribute("team", team);
        return "team/user";
    }

    /**
     * 普通用户查看
     * @param team
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list-query")
    public String query(Team team, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 10);
        Example example = new Example(Team.class);
        if(team.getName()!=null)
            example.createCriteria()
                    .andLike("name", "%" + team.getName() + "%");
        PageInfo<Team> pageInfo = new PageInfo<>(teamMapper.selectByExample(example), 5);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("requestUrl", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "team/query";
    }

    /**
     * 查看球队信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("detail-query/{id}")
    public String detail_query(@PathVariable Integer id, Model model) {
        Player player = new Player();
        player.setTeamid(id);
        List<Player> playerList = playerMapper.select(player);
        model.addAttribute("playerList", playerList);
        Team team = teamMapper.selectByPrimaryKey(id);
        model.addAttribute("team", team);
        return "team/detail-query";
    }
}
